#include <stdio.h>
//
// Created by win10 on 2024/12/11.
//
int binarySearch(int arr[], int size, int key) {
    int low = 0;
    int high = size - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (arr[mid] == key) {
            return mid;
        }else if (arr[mid] < key) {
            low = mid + 1;
        }else {
            high = mid - 1;
        }
    }
    return -1;
}

//递归474964
int binarySearchRec(int arr[], int low, int high, int key) {
    if (low > high) {
        return -1;
    }
    int mid = (low + high) / 2;
    if (arr[mid] == key) {
        return mid;
    }
    if (arr[mid] < key) {
        return binarySearchRec(arr, low, mid - 1, key);
    }
    return binarySearchRec(arr, mid + 1, high, key);

}
int main(){
    int arr[] = {1,3,5,7,9,11,13,17,19};
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", binarySearch(arr, size, 1));
    return 0;
}